package com.woniuxy.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.store.infra.mapper.MemberLevelMapper;
import com.woniuxy.store.infra.model.MemberLevel;
import com.woniuxy.store.service.MemberLevelService;
import exception.MallException;
import org.springframework.stereotype.Service;
import result.CommonResultCodeEnum;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zz
 * @since 2023-07-20
 */
@Service
public class MemberLevelServiceImpl extends ServiceImpl<MemberLevelMapper, MemberLevel> implements MemberLevelService {

    @Override
    public void addMemberLevel(MemberLevel level) {
        QueryWrapper<MemberLevel> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id",level.getStoreId()).eq("level",level.getLevel());
        MemberLevel one = getOne(wrapper);
        if (one!=null){
            throw new MallException("该等级已存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        save(level);
    }

    @Override
    public void modifyMemberLevel(MemberLevel level) {
        MemberLevel memberLevel = getById(level.getMemberLevelId());
        if (memberLevel==null||memberLevel.getStoreId().intValue()!= level.getStoreId().intValue()){
            throw new MallException("修改等级不存在",CommonResultCodeEnum.INVALID_PARAM);
        }
        updateById(level);
    }

    @Override
    public void delMemberLevel(Integer storeId, Integer memberLevelId) {
        MemberLevel memberLevel = getById(memberLevelId);
        if (memberLevel==null||memberLevel.getStoreId().intValue()!= storeId.intValue()){
            throw new MallException("删除等级不存在",CommonResultCodeEnum.INVALID_PARAM);
        }
        removeById(memberLevelId);
    }

    @Override
    public List<MemberLevel> getMemberLevel(Integer storeId) {
        QueryWrapper<MemberLevel> wrapper = new QueryWrapper<>();
        wrapper.eq("store_id",storeId);
        List<MemberLevel> list = list(wrapper);
        return list;
    }


}
